package com.lao5net.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;
import org.apache.commons.lang.time.DateUtils;

public final class DateUtil {

    /**
     * 判断时间是否越界
     * 
     * @param time
     * @return
     */

    public static boolean checkStandardTime(Date time) {
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:ss:mm");
        try {
            Date standard = format.parse("1970-01-02 00:00:00");
            if (time != null) {
                return standard.before(time);
            } else {
                return false;
            }
        } catch (ParseException e) {

            return false;
        }
    }

    private DateUtil() {
    }

    /**
     * 格式化日期 pattern:yyyy-MM-dd HH:mm:ss
     * 
     * @param date
     * @return
     */
    public static String formatDate(Date date) {
        return formatDate(date, "yyyy-MM-dd HH:mm:ss");
    }

    /**
     * 格式化日期 pattern:yyyyMMdd
     * 
     * @param date
     * @return
     */
    public static String getDay(Date date) {
        return formatDate(date, "yyyyMMdd");
    }

    /**
     * 格式化日期 pattern:yyyy-MM-dd
     * 
     * @param date
     * @return
     */
    public static String formatDay(Date date) {
        return formatDate(date, "yyyy-MM-dd");
    }
    
    /**
     * 格式化日期 pattern:yyyy-MM-dd
     * 
     * @param date
     * @return
     */
    public static String formatFullDay(Date date) {
        return formatDate(date, "yyyyMMddHHmmss");
    }

   
    public static Date prase(String date, String pattern) throws ParseException {
        DateFormat df = new SimpleDateFormat(pattern);
        return df.parse(date);
    }

    /**
     * 格式化日期
     * 
     * @param date
     * @param pattern
     * @return
     */
    public static String formatDate(Date date, String pattern) {
        if (date == null) {
            return null;
        }
        DateFormat df = new SimpleDateFormat(pattern);
        return df.format(date);
    }

    /**
     * 今天开始时间,如：2011-12-16 00:00:00
     * 
     * @return
     */
    public static Date getTodayBegin() {
        Calendar cal = Calendar.getInstance();
        clearTime(cal);
        return cal.getTime();
    }

    public static Date getDayBegin(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        clearTime(cal);
        return cal.getTime();
    }

    /**
     * 某一日的结束时间, 如: 2011-12-16 23:59:59
     * 
     * @param date
     * @return
     */
    public static Date getDayEnd(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.AM_PM, Calendar.AM);
        cal.set(Calendar.HOUR, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        return cal.getTime();
    }

    /**
     * 本周开始时间,如：2011-12-12 00:00:00
     * 
     * @return
     */
    public static Date getWeekBegin() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        clearTime(cal);
        return cal.getTime();
    }

    /**
     * 本月开始时间,如：2011-12-01 00:00:00
     * 
     * @return
     */
    public static Date getMonthBegin() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.DAY_OF_MONTH, 1);
        clearTime(cal);
        return cal.getTime();
    }

    /**
     * 上月开始时间
     * 
     * @return
     */
    public static Date getLastMonthBegin() {
        return DateUtils.addMonths(getMonthBegin(), -1);
    }

    /**
     * 上月结束时间
     * 
     * @return
     */
    public static Date getLastMonthEnd() {
        Date retval = DateUtils.addMonths(getMonthBegin(), -1);
        Calendar cal = Calendar.getInstance();
        cal.setTime(retval);
        int days = cal.getMaximum(Calendar.DATE);
        cal.set(Calendar.DATE, days);
        cal.set(Calendar.AM_PM, Calendar.PM);
        cal.set(Calendar.HOUR, 11);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        return cal.getTime();
    }

    private static void clearTime(Calendar cal) {
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
    }

    public static Date getDateByHour(Date d1, int hour) {
        Calendar calendar = Calendar.getInstance();
        if (d1 != null) {
            calendar.setTime(d1);
        }

        int i = calendar.get(Calendar.HOUR);
        i = i + hour;
        calendar.set(Calendar.HOUR, i);
        return calendar.getTime();
    }

    public static Date getDateByYear(Date d1, int years) {
        Calendar calendar = Calendar.getInstance();
        if (d1 != null) {
            calendar.setTime(d1);
        }

        int i = calendar.get(Calendar.YEAR);
        i = i + years;
        calendar.set(Calendar.YEAR, i);
        return calendar.getTime();
    }

    public static Date getDateByDay(Date d1, int days) {
        Calendar calendar = Calendar.getInstance();
        if (d1 != null) {
            calendar.setTime(d1);
        }

        int i = calendar.get(Calendar.DATE);
        i = i + days;
        calendar.set(Calendar.DATE, i);
        return calendar.getTime();
    }

    /**
     * 通过滚动month获得date
     * 
     * @param d1
     * @param month
     * @return
     */
    public static Date getDateByMonth(Date d1, int month) {
        Calendar calendar = Calendar.getInstance();
        if (d1 != null) {
            calendar.setTime(d1);
        }

        int i = calendar.get(Calendar.MONTH);
        i = i + month;
        calendar.set(Calendar.MONTH, i);
        return calendar.getTime();
    }

    /**
     * 计算当前时间在给定的时间范围内已流逝的比例
     * 
     * @param starDate
     * @param endDate
     * @return
     */
    public static float calRatePass(Date starDate, Date endDate) {
        if (starDate == null || endDate == null) {
            return 0.0f;
        }
        Calendar starCal = Calendar.getInstance();
        starCal.setTime(starDate);
        clearTime(starCal);

        Calendar endCal = Calendar.getInstance();
        endCal.setTime(endDate);
        clearTime(endCal);

        Calendar curCal = Calendar.getInstance();
        clearTime(curCal);

        long total = endCal.getTimeInMillis() - starCal.getTimeInMillis();
        long pass = curCal.getTimeInMillis() - starCal.getTimeInMillis();

        return (float) pass / total;
    }

    /**
     * 获得一个很久很久以后的时间
     * 
     * @return
     */
    public static Date getLongLongTimeAfter() {
        //一万年太久，只争朝夕
        return DateUtils.addYears(new Date(), 10000);
    }

    /**
     * 获得当前时间前前 range 天内的一个随机时间 <br>
     * 时间段： 9:00-23:59:59
     * 
     * @param range
     * @return
     */
    public static Date getRandomDate(int range) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());

        Random random = new Random(System.currentTimeMillis());
        int begin = random.nextInt(range);
        cal.add(Calendar.DATE, 0 - begin);

        random = new Random(System.currentTimeMillis());
        int hour = random.nextInt(14);
        hour = 9 + hour; // 从九点开始
        cal.set(Calendar.HOUR_OF_DAY, hour);

        random = new Random(System.currentTimeMillis());
        int minutes = random.nextInt(59);
        cal.set(Calendar.MINUTE, minutes);

        random = new Random(System.currentTimeMillis());
        int second = random.nextInt(59);
        cal.set(Calendar.SECOND, second);

        return cal.getTime();
    }

    /**
     * 计算小时数
     * 
     * @param start
     * @param end
     * @return
     */
    public static long calculateHours(Date start, Date end) {
        if (start == null || end == null) {
            throw new IllegalArgumentException("start or end could not be null");
        }

        if (start.after(end)) {
            return 0;
        }
        long result = end.getTime() - start.getTime();

        return result / (1000 * 60 * 60);
    }

    /**
     * 计算年数,粗略计算
     * 
     * @param betweenHours
     * @return
     */
    public static long calculateYear(long betweenHours) {
        return betweenHours / (24 * 365);
    }

    /**
     * 计算年数,粗略计算
     * 
     * @param betweenHours
     * @return
     */
    public static long calculateYear(Date start, Date end) {
        return calculateHours(start, end) / (24 * 365);
    }

    public static Date createDate(int year, int month, int day) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month, day, 0, 0, 0);
        return calendar.getTime();
    }

    /**
     * yyyy-MM-dd 00:00:00 >>> yyyy-MM-dd 23:59:59
     * 
     * @param date
     * @return
     */
    public static Date format24(Date date) {

        return getDayEnd(date);
    }

    /**
     * 时间滚动操作
     * 
     * @param date
     * @param type
     * @param tep
     * @return
     */
    public static Date rollDateTime(Date date, int type, int tep) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(date.getTime());
        calendar.add(type, 3);
        return calendar.getTime();
    }

}
